package com.haidong.myfile;

import java.util.ArrayList;
import java.util.List;

/**
 * 负责运行的主类
 */
public class Index {
    //使用静态代码块初始化三个盘符,七个文件夹,四个用户组,十个用户
    private static MyFile diskC,diskD,diskE;
    private static MyFile f1,f2,f3,f4,f5,f6,f7,f8,f9;
    private static Group develop,run,test,visitor;
    private static List<User> users = new ArrayList<>();
    private static int times = 0;
    private static String name,password;
    static {
        develop = new Group("开发组");
        run = new Group("运维组");
        test = new Group("测试组");
        visitor = new Group("游客组");
        users.add(new User("root","123456",develop));
        users.add(new User("徐峥","123456",develop));
        users.add(new User("葛优","123456",develop));
        users.add(new User("谢广坤","123456",develop));
        users.add(new User("林黛玉","123456",run));
        users.add(new User("张曼玉","123456",run));
        users.add(new User("张学友","123456",test));
        users.add(new User("林青霞","123456",test));
        users.add(new User("猪八戒","123456",test));
        users.add(new User("刘德华","123456",test));
        diskC = new MyFile("C");
        diskD = new MyFile("D");
        diskE = new MyFile("E");
        f1 = new MyFile("home",diskC);
        f2 = new MyFile("src",diskC);
        f3 = new MyFile("test",f1);
        f4 = new MyFile("data",diskD);
        f5 = new MyFile("image",diskD);
        f6 = new MyFile("game",diskE);
        f7 = new MyFile("music",diskE);
        f8 = new MyFile("test2",f1);
        f9 = new MyFile("str.txt",f2,users.get(0));
    }

    //初始化设置当前目录为C:\home
    private static MyFile nowPath = f1,writeSon;
    private static User nowUser;
    public static void main(String[] args) throws Exception {
        MyFrame myframe = new MyFrame("终端框");
    }

    //这是一个超大方法,主要作用就是通过被监听器调用,然后和文本域数据交互
    public static String getListenText(String text){
        if(times ==0){
            name = text.substring(9);
            times++;
            return "请输入您的密码:";
        }else if(times ==1){
            password = text.substring(8);
            for (User user : users) {
                if(user.judge(name,password)){
                    nowUser = user;
                    times++;
                    return "登录成功!欢迎您^_^\n";
                }
            }
            times = 0;
            return "登录失败!请重试:";
        }else{
            times++;
            //分析监听器发送的文本信息,翻译成指令执行

            //根据返回值判断是要写入文件
            if(text.startsWith("请输入写入的内容:")){
                writeSon.write2(text.substring(9));
            }

            //将传入的文本先用>分隔开当前地址名,然后再用空格分割将命令和数据分开
            String[] orders = text.split(">");
            String order = orders[orders.length-1];
            String[] commands = order.split(" ");

            //如果是单命令且是exit执行退出
            if(commands.length==1){
                if("exit".equals(commands[0])){
                    System.exit(0);
                }
            }else if(commands.length==2){
                //双命令格式较多,cd,md,rd,dir,edit,delete,read和write都是
                //通过字符串匹配选择对应的功能执行,目录操作的时候可以用..代替父目录
                if("cd".equals(commands[0])){
                    if("..".equals(commands[1])){
                        nowPath = nowPath.getFather();
                    }else if("D".equals(commands[1])){
                        nowPath = diskD;
                    }else if("E".equals(commands[1])){
                        nowPath = diskE;
                    }else if("C".equals(commands[1])){
                        nowPath = diskC;
                    }
                    else{
                        for (MyFile son : nowPath.getSons()) {
                            if(commands[1].equals(son.getName())){
                                nowPath = son;
                                break;
                            }
                        }
                    }
                }else if("dir".equals(commands[0])){
                    if("..".equals(commands[1])){
                        return nowUser.dir(nowPath);
                    }else{
                        for (MyFile son : nowPath.getSons()) {
                            if(commands[1].equals(son.getName())){
                                return nowUser.dir(son);
                            }
                        }
                    }
                }else if("md".equals(commands[0])){
                    //当前目录下有无同名,有就不添加并返回消息
                    List<MyFile> sons = nowPath.getSons();
                    for (MyFile son : sons) {
                        if(son.getName().equals(commands[1])){
                            return "该文件已存在\n";
                        }
                    }
                    nowPath = new MyFile(commands[1],nowPath);
                }else if("rd".equals(commands[0])){
                    if("..".equals(commands[1]) && nowPath.getFather()!=null){
                        MyFile temp = nowPath;
                        nowPath = nowPath.getFather();
                        nowPath.getSons().remove(temp);
                    }else{
                        List<MyFile> sons = nowPath.getSons();
                        for (int i = 0;i < sons.size();i++) {
                            if(commands[1].equals(sons.get(i).getName())){
                                nowPath.getSons().remove(sons.get(i));
                                break;
                            }
                        }
                    }
                }else if("edit".equals(commands[0])){
                    //当前目录下有无同名,有就不添加
                    List<MyFile> sons = nowPath.getSons();
                    for (MyFile son : sons) {
                        if(son.getName().equals(commands[1])){
                            return "该文件已存在\n";
                        }
                    }
                    nowUser.create(commands[1], nowPath).getData();
                }else if("delete".equals(commands[0])){
                    nowUser.delete(commands[1],nowPath);
                }else if("read".equals(commands[0])){
                    List<MyFile> sons = nowPath.getSons();
                    for (int i = 0;i < sons.size();i++) {
                        if(commands[1].equals(sons.get(i).getName()) && sons.get(i).isFile==1){
                            return sons.get(i).read(nowUser);
                        }
                    }
                }else if("write".equals(commands[0])){
                    //在这里调用write获取文件的授权,然后调用write2
                    List<MyFile> sons = nowPath.getSons();
                    for (int i = 0;i < sons.size();i++) {
                        if(commands[1].equals(sons.get(i).getName())){
                            String writePermit = sons.get(i).write(nowUser);
                            if("请输入写入的内容:".equals(writePermit)){
                                writeSon = sons.get(i);
                            }
                            return writePermit;
                        }
                    }
                }

            }else if(commands.length==3){
                //三命令只有两种,一种是切换用户,一种是给文件赋予新权限
                if("login".equals(commands[0])){
                    for (User user : users) {
                        if(user.judge(commands[1],commands[2])){
                            nowUser = user;
                            return "登录成功!欢迎您^_^\n";
                        }
                    }
                }else if("chmod".equals(commands[0]) && commands[2].length()==9){
                    List<MyFile> sons = nowPath.getSons();
                    for (int i = 0;i < sons.size();i++) {
                        if(commands[1].equals(sons.get(i).getName())){
                            if(sons.get(i).getUser()==nowUser){
                                int[][] permit = new int[3][3];
                                for(int j = 0;j < 3;j++){
                                    for(int k = 0;k < 3;k++){
                                        if("0".equals(commands[2].charAt(j*3+k) + "")){
                                            permit[j][k] = 0;
                                        }else permit[j][k] = 1;
                                    }
                                }
                                sons.get(i).setPermission(permit);
                            }else return "只有文件创建者才能修改访问权限!\n";
                        }
                    }
                }
            }
            //如果没有需要输出到文本域的内容就把当前路径输出到行首
            return nowPath.showPath() + ">";
        }
    }
}
